using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using logistical.Models;

namespace logistical.Controllers
{
    public class base_psersonalFilesController : baseController
    {
        public class list_base_psersonalFiles
        {
            public List<base_psersonalFiles> objs { get; set; }
        }

        [HttpGet]
        public object get()
        {
            try
            {
                var o = new logisticalEntities();
                var p = o.base_psersonalFiles;
                var personalType = o.sys_iflag.Where(x => x.typecode == "personalType").Select(x => new
                {
                    id = x.code,
                    text = x.name
                });
                var iflag = o.sys_iflag.Where(x => x.typecode == "sys").Select(x => new
                {
                    id = x.code,
                    text = x.name
                });

                return new
                {
                    data = p
,
                    personalType,
                    iflag
                };
            }
            catch (Exception ex)
            {
                return new { msg = ex.Message };
            }
        }


        [HttpPost]
        public object add(base_psersonalFiles obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                obj.id = Guid.NewGuid().ToString();
                common.sysEval.evalCurUserAndDate(obj);
                o.base_psersonalFiles.Add(obj);
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object edit(base_psersonalFiles obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.base_psersonalFiles.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.base_psersonalFiles.SingleOrDefault(x => x.id == obj.id);
                    old.code = obj.code;
                    old.name = obj.name;
                    old.idcard = obj.idcard;
                    old.workingTime = obj.workingTime;
                    old.mobile = obj.mobile;
                    old.userid = obj.userid;
                    old.personalType = obj.personalType;
                    old.carNo = obj.carNo;
                    old.postStr = obj.postStr;
                    old.iflag = obj.iflag;
                    old.remark = obj.remark;
                    common.sysEval.evalCurUserAndDate(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object del(base_psersonalFiles obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.base_psersonalFiles.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.base_psersonalFiles.SingleOrDefault(x => x.id == obj.id);
                    o.base_psersonalFiles.Remove(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object mdel(list_base_psersonalFiles objs)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                foreach (var p in objs.objs)
                {
                    var co = o.base_psersonalFiles.Where(x => x.id == p.id);
                    if (co.Count() == 1)
                    {
                        var old = o.base_psersonalFiles.SingleOrDefault(x => x.id == p.id);
                        o.base_psersonalFiles.Remove(old);
                    }
                }
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }
    }
}
